/** @file    wCore_Table.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_Table.cpp 2808 2010-07-28 20:44:00Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_Table class.
 * The header for this class can be found in wCore_Table.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this table of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this table of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_Table.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wCore_Table, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Table )
 WOSH_GUI_MAP(wosh::gui::wCore_Table, "wosh::Table", 0.0, _static_Map_wCore_Table )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wCore_Table::wCore_Table( QWidget* parent )
	: QTableView(parent), tableModel(NULL) {

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	this->tableModel = new TableModel_Core_Table(this);
	QTableView::setModel(this->tableModel);
//setSortingEnabled(true);

 }

wCore_Table::~wCore_Table() {
	if ( this->tableModel != NULL )
		delete this->tableModel; this->tableModel = NULL;
 }


WRESULT wCore_Table::loadObject( const wosh::Table* object ) {
	return this->tableModel->loadObject(object);
 }

WRESULT wCore_Table::saveObject( wosh::Table* object ) const {
	if ( object == NULL ) return WRET_ERR_PARAM;

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

TableModel_Core_Table::TableModel_Core_Table(QObject *parent)
	: QAbstractTableModel(parent), dataTable(NULL) {

 }

TableModel_Core_Table::~TableModel_Core_Table() {
	if ( this->dataTable != NULL )
		delete this->dataTable; this->dataTable = NULL;
 }

WRESULT TableModel_Core_Table::loadObject( const wosh::Table* table ) {
	if ( this->dataTable != NULL )
		delete this->dataTable; this->dataTable = NULL;
	if ( table == NULL )
		return WRET_OK;
	this->dataTable = table->clone();
	reset();
	return WRET_OK;
 }

WRESULT TableModel_Core_Table::acquireObject( wosh::Table* table ) {
	if ( this->dataTable != NULL )
		delete this->dataTable; this->dataTable = NULL;
	this->dataTable = table;
	reset();
	return WRET_OK;
 }

int TableModel_Core_Table::rowCount(const QModelIndex &parent ) const {
	(void)parent;
	if ( this->dataTable == NULL ) return 0;
	return this->dataTable->getRows();
 }

int TableModel_Core_Table::columnCount(const QModelIndex &parent ) const {
	(void)parent;
	if ( this->dataTable == NULL ) return 0;
	return this->dataTable->getCols();
 }

QVariant TableModel_Core_Table::data(const QModelIndex &index, int role) const {
	if ( this->dataTable == NULL || !index.isValid() ) return QVariant();
	if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
		const ISerializable* object = this->dataTable->get(index.row(), index.column() );
		if ( object == NULL )
			return QVariant();
		return GuiUtilities::convertSerializable2QVariant(object);
	 }
	else if ( role == Qt::BackgroundColorRole ) {
	 }
	else if ( role == Qt::ToolTipRole ) {
		const ISerializable* object = this->dataTable->get(index.row(), index.column() );
		if ( object == NULL )
			return QVariant("NULL Object");
		if ( object->isKindOf<Variant>() ){
			const wosh::Variant* dataObject = dynamic_cast<const wosh::Variant*>(object);
			return QString("wosh::Variant [%1]").arg(dataObject->getTypeAsString());
		 }
		return QVariant(object->getClassName());
	 }
	return QVariant();
 }

QVariant TableModel_Core_Table::headerData(int section, Qt::Orientation orientation, int role) const {
	if ( this->dataTable == NULL ) return QVariant();
	if (role != Qt::DisplayRole) return QVariant();
	 if (orientation == Qt::Horizontal ) {
		 if ( this->dataTable->hasHeaderHorizontal() ) {
			ISerializable* object = this->dataTable->getHeaderHorizontal(section);
			if ( object == NULL )
				return QString("Col%1").arg(section);
			if ( object->isKindOf<Variant>() ) {
				wosh::Variant* dataObject = dynamic_cast<wosh::Variant*>(object);
				return QVariant(dataObject->toString().c_str());
			 }
			else
				return QVariant(object->toString().c_str());
		 }
		//return QString("Col%1").arg(section);
	 }
	else if (orientation == Qt::Vertical ) {
		if ( this->dataTable->hasHeaderVertical() ) {
			ISerializable* object = this->dataTable->getHeaderVertical(section);
			if ( object == NULL )
				return QString("Row%1").arg(section);
			if ( object->isKindOf<Variant>() ) {
				wosh::Variant* dataObject = dynamic_cast<wosh::Variant*>(object);
				return QVariant(dataObject->toString().c_str());
			 }
			else
				return QVariant(object->toString().c_str());
		 }
		//return QString("Row%1").arg(section);
	 }
	return QVariant();
 }

void TableModel_Core_Table::sort( int column, Qt::SortOrder order ) {
	if ( this->dataTable == NULL ) return;
	if ( column < 0 ) return;
	this->dataTable->sortByColumn((unsigned int)column, (short)order);
	reset();
 }

 Qt::ItemFlags TableModel_Core_Table::flags(const QModelIndex &index) const {
	if ( this->dataTable == NULL || !index.isValid() )
		 return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
	return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
  }

bool TableModel_Core_Table::setData(const QModelIndex &index, const QVariant &value, int role) {
	if ( this->dataTable == NULL ) return false;
	if ( !index.isValid() || role != Qt::EditRole) return false;

	ISerializable* object = this->dataTable->get(index.row(), index.column() );
	if ( object == NULL )
		return false;

	if ( object->isKindOf<Variant>() ) {
		wosh::Variant* dataObject = dynamic_cast<wosh::Variant*>(object);
		GuiUtilities::convertQVariant2Variant( dataObject, value );
		emit dataChanged(index, index);
		emit tableCell_changed(index.row(), index.column(), object );
		return true;
	 }
	 return false;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 }; // namespace gui
}; // namespace wosh
